package com.icode.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.icode.common.valid.AddGroup;
import com.icode.common.valid.UpdateGroup;
import com.icode.common.valid.UpdateStatusGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.icode.gulimall.product.entity.BrandEntity;
import com.icode.gulimall.product.service.BrandService;
import com.icode.common.utils.PageUtils;
import com.icode.common.utils.R;



/**
 * 品牌
 *
 * @author my_code
 * @email 1345743081@gmail.com
 * @date 2020-10-27 19:38:13
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    public R info(@PathVariable("brandId") Long brandId) {
        BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    @GetMapping("/allInfo")
    public R info(@RequestParam("brandIds") List<Long> brandIds) {
        List<BrandEntity> brands = brandService.getByIds(brandIds);

        return R.ok().put("brands", brands);
    }
    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@Validated({AddGroup.class}) @RequestBody BrandEntity brand/*, BindingResult result*/) {
		/*if(result.hasErrors()){
		    Map<String, String> map =new HashMap<>();
		    //1、获取校验错误的结果
		    result.getFieldErrors().forEach((item)->{
		        //FieldError获取错误的提示
		        String message =item.getDefaultMessage();
		        //获取错误的属性的名字
		        String filed = item.getField();
		        map.put(filed, message);
            });
		    return R.error(400,"提交数据不合法").put("data", map);
        }else{
            brandService.save(brand);
        }*/
        brandService.save(brand);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@Validated({UpdateGroup.class}) @RequestBody BrandEntity brand) {
        brandService.updateDatail(brand);
        return R.ok();
    }
    /**
     * 修改
     */
    @RequestMapping("/updateStatus")
    public R updateStatus(@Validated({UpdateStatusGroup.class}) @RequestBody BrandEntity brand) {
        brandService.updateById(brand);
        return R.ok();
    }
    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] brandIds) {
        brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

}
